package com.healthcarex.hip.saas.message.model;

import com.healthcarex.hip.saas.common.util.xml.CDATASectionAdapter;
import lombok.Data;

import javax.xml.bind.annotation.*;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;

/**
 * *******description*******
 * healthcarex hip报文
 * <p>
 * 报文格式如下
 * ============
 * <MSG>
 * <HEAD>
 * <!--请求号-->
 * <req_id></req_id>
 * <!--签名方式 [RSA MD5 Cleartext(明文)]-->
 * <sec_id></sec_id>
 * <!--密钥-->
 * <sec_key></sec_key>
 * <!--接口名称-->
 * <service></service>
 * <!--请求参数格式 [xml json string]-->
 * <format></format>
 * <!-- 合作者身份-->
 * <partner></partner>
 * <!--创建时间 （使用long数据类型表示时间）-->
 * <createTime></createTime>
 * <!--消息类型 {V2(版本2) V3(版本3) TEST(测试用消息类型只返回是否收到信息，不做任何处理)}-->
 * <MsgType></MsgType>
 * <!--发送方在系统中的注册号-->
 * <Ticket></Ticket>
 * <!--事件类型-->
 * <Event></Event>
 * <!--事件Key-->
 * <Event_key></Event_key>
 * </HEAD>
 * <BODY>
 * <CONTENT><![CDATA[用户XML报文信息]]></CONTENT>
 * </BODY>
 * </MSG>
 * ===========
 * *************************
 *
 * @author ChenPan
 * @date 2018/8/29 15:38
 */
@XmlType(propOrder = {"head", "body"})
@XmlRootElement(name = "MSG")
@XmlAccessorType(XmlAccessType.FIELD)
@XmlAccessorOrder(XmlAccessOrder.ALPHABETICAL)
@Data
public class HIPMSG {

    public enum FormatType {
        xml, json, string
    }

    public enum MsgType {
        V2, V3, TEST
    }

    @XmlElement(name = "HEAD")
    private Head head;

    @XmlElement(name = "BODY")
    private Body body;

    public HIPMSG.Head createHead() {
        Head head = new Head();
        this.setHead(head);
        return head;
    }

    public HIPMSG.Body createBody(String content) {
        Body body = new Body();
        body.setContent(content);
        this.setBody(body);
        return body;
    }

    @Data
    @XmlAccessorType(XmlAccessType.FIELD)
    @XmlType(propOrder = {"reqId", "secId", "secKey", "service", "format", "partner", "createTime",
            "msgType", "ticket", "event", "eventKey"})
    public static class Head {

        /**
         * 请求号
         */
        @XmlElement(name = "req_id")
        private String reqId = "";

        /**
         * 签名方式
         */
        @XmlElement(name = "sec_id")
        private String secId = "";

        /**
         * 密钥
         */
        @XmlElement(name = "sec_key")
        private String secKey = "";

        /**
         * 接口名称
         */
        @XmlElement(name = "service")
        private String service = "";

        /**
         * 请求参数格式
         */
        @XmlElement(name = "format")
        private String format = "";

        /**
         * 合作者身份
         */
        @XmlElement(name = "partner")
        private String partner = "";

        /**
         * 创建时间
         */
        @XmlElement(name = "createTime")
        private long createTime;

        /**
         * 消息类型
         */
        @XmlElement(name = "MsgType")
        private String msgType = "";

        /**
         * 发送方在系统中的注册号
         */
        @XmlElement(name = "Ticket")
        private String ticket = "";

        /**
         * 事件类型
         */
        @XmlElement(name = "Event")
        private String event = "";

        /**
         * 事件Key
         */
        @XmlElement(name = "Event_key")
        private String eventKey = "";

    }

    @Data
    @XmlAccessorType(XmlAccessType.FIELD)
    public static class Body {

        /**
         * 报文信息
         */
        @XmlElement(name = "CONTENT")

        @XmlJavaTypeAdapter(CDATASectionAdapter.class)
        private String content = "";
    }


}
